<?php
/**
 * Created by PhpStorm.
 * User: ASUS
 * Date: 2019/10/17
 * Time: 14:59
 */
namespace app\phone\controller;

use app\common\controller\BaseController;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Index extends Controller {
    public function api(){
        $request=Request::instance();
        $sid=$request->param('sid');
        $sk=$request->param('sk');
        session_id($sid);
        session_start();
        $openid=Session::get($sk);
//        $arr=Db::table('p_student')->where('openid',Session::get($sk))->find();
          $arr=Db::table('p_student')->alias('s')->field('s.id,s.name,s.tel,s.c_id,s.n_id,c.class,x.xueqi')
              ->join('p_class c','c.c_id=s.c_id','left')->join('p_xueqi x','x.n_id=s.n_id','left')
              ->where('s.openid',$openid)->find();
        if($arr){
            echo json_encode($arr);
        }else{
            $data=['openid'=>$openid];
            Db::table('p_student')->insert($data);
        }
//
    }
    public function upt(){
        $request=Request::instance();
        $id=$request->param('id');
        $arr=Db::table('p_student')->alias('s')->field('s.id,s.name,s.tel,s.c_id,s.n_id,c.class,x.xueqi')
            ->join('p_class c','c.c_id=s.c_id','left')->join('p_xueqi x','x.n_id=s.n_id','left')
            ->where('s.id',$id)->find();
        echo json_encode($arr);
    }
    public function xueqi(){
        $arr=Db::table('p_xueqi')->select();
        echo json_encode($arr);
    }
    public function banji(){
        $arr=Db::table('p_class')->select();
        echo json_encode($arr);
    }
    public function index(){
            $arr=Db::table('p_class')->select();
            echo json_encode($arr);
    }
    public function lists(){
        $request=Request::instance();
        $c_id=$request->param('c_id');
        $n_id=$request->param('n_id');
        $data=$request->param("key");
        $p=$request->param('page');
        if($c_id){
            $arr=Db::table('p_student')->where('c_id',$c_id)->page($p,10)->select();
            echo json_encode($arr);
//            echo $c_id;
            exit;
        }
        if($n_id){
            $arr=Db::table('p_student')->where('n_id',$n_id)->page($p,3)->select();
            echo json_encode($arr);
        }
        if($data){
            $arr=Db::table('p_student')->where('name','like','%'.$data.'%')->page($p,3)->select();
            if($arr){
                echo json_encode($arr);
            }else{
                echo 123;
            }
        }
    }
    public function login(){
        $request=Request::instance();
        $code=$request->param('code');
        $appid="wx1812ad1de352a28c";
        $secret="f6310620dd48f4fad69ec63a73d4ebc2";
        $url="https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
        $rt=https_request($url);
        $arr=json_decode($rt,true);
        Session::set($arr['session_key'],$arr['openid']);
        echo json_encode(['sk'=>$arr['session_key'],'sid'=>session_id()]);
        exit;
    }
}